How to handle scientific articles/reports
Introduction and objectives
This lab work addresses the issue of conducting an experiment and reporting it. The point is to use smart tools, so that the report, the figures, the experiment outputs, the parameters in the experiment and the parameters printed in the report are always kept coherent with a minimal effort from you.
Here, let us consider a fake project. You may see, afterwards, that it is indeed quite general and what you will have learned with this fake project can easily be reused in your personal real-life experiments.
After the lab work, you will see how command line tools, LaTeX and make can be involved for a safe, easy, and professional reporting of your experiments (i.e. scientific articles, industrial technical reports, etc.).
Install
On ubuntu, you will need the following packages.
mylogin@mymachine:~$ sudo apt install latexmk texlive-latex-recommended texlive-lang-french texlive-fonts-extra texlive-science make python3-venv tree
Getting started
Makefiles
You have to understand the basics of the make command for this labwork. Read only the "Main principle" section make tutorial from the C++ lecture (and do what your told there), do not read the "Compiling C++" section and others.
Get your project
We provide you with a (quite) empty project. This is typically something that would be a git repository for your a specific publication. Download my-research.tar.gz, uncompress it and then enter that directory.
For now, there is only a makefile available. Before reading it, try one by one these commands:
mylogin@mymachine:~$ make enter MESSAGE="hello world"
mylogin@mymachine:~$ make leave MESSAGE="hello world"
mylogin@mymachine:~$ ls
mylogin@mymachine:~$ make init-build
mylogin@mymachine:~$ ls
mylogin@mymachine:~$ make init-venv
mylogin@mymachine:~$ ls
mylogin@mymachine:~$ make init
As we have initialized a virtual environment, let us activate it.
Now, you can read the makefile. Ask for help, you have to understand each line. The activate-virtual-env target is a bit tricky.
Cleaning
Write a clean target that removes all unnecessary files (the ones that can be regenerated, the temporary files, etc.).
Be sure to update this target throughout this labwork.
Set up your experiments
Managing the python package
You research is made of running experiments that produces figures. Let is rely on python. Avoid single-piece code, it is better to have a library (or more... but let us consider one here) where all your computation is nicely organized. Making a figure is a quite small script that invokes few calls of some functions in your library.
The library, as well as the experiments, will be placed in an experiment subdirectory. Go into the my-research directory (if you are not already there), and then
We have written a library for you, download lib.tar.gz somewhere (denoted by <somewhere> hereafter), and uncompress this into the my-research/experiments directory (you are there).
Let us display the content of the lib directory.
Or more nicely
This directory contains what you need for installing the package expe. Read pyproject.toml (you will easily understand what it is about), then read the python files in the expe directory. These are our main python code.
Making a package and installing it in our environment (that should be currently activated) is very easy with pip.
You can test the install by going elsewhere, and lauch a python interpretor
And then try to import expe...
... this should work without errors. Then go back in your my-research directory (use cd).
Now, in your makefile add a target update-lib, that invokes pip in order to install your lib. Use variables for directories, as we did with BUILD_DIR. The command...
...should re-install the package. Try to modify the target such as the package is re-build only if some of the python files has been modified. You can use the wildcard function of make to mention all the python sources (google it).
Managing figures
Go into the experiment directory, and uncompress figures.tar.gz as you did with lib.tar.gz. Try the figure script.
Read the fig-lorentzorbits.py file. Note that the figure generation mainly relies on our expe package, as well as the params.py script. Indeed, this parameter file can be executed.
Read the generated params.tex file as well as the params.py file.
Add a $(BUILD_DIR)/fig-lorentzorbits.pdf target in your makefile, for generating the figure. It should rebuild the figure if
- params.py is modified
- fig-lorentzorbits.py is modified
- the expe package is modified.
In the future, you may want to generate other figures, using the same rationale. So what we did for fig-lorentzorbits.pdf will have to be done for any fig-xxx.pdf figure. Modify the rule for fig-lorentzorbits.pdf so that it becomes applicable to any fig-xxx.pdf. Use makefile "pattern rules" to do so (google it !).
Using latex
Writing an article
Create a directory tex-sources in your project (i.e. my-research/text-sources). Add a SRC_TEX_DIR variable for this path in your makefile.
In your makefile, add the following definitions and target
COMMON_TEX_SOURCES = \
$(SRC_TEX_DIR)/title-matter.tex \
$(SRC_TEX_DIR)/equations.tex \
$(SRC_TEX_DIR)/params.tex
PAPER_SOURCES = \
$(COMMON_TEX_SOURCES) \
$(SRC_TEX_DIR)/paper.tex \
$(SRC_TEX_DIR)/paper-orbit.tex
COMMON_FIGURES = \
$(BUILD_DIR)/fig-lorentzorbits.pdf
PAPER_FIGURES = \
$(COMMON_FIGURES)
paper.pdf: $(PAPER_SOURCES) $(PAPER_FIGURES)
@make enter --quiet MESSAGE="Compiling paper"
cd $(SRC_TEX_DIR); latexmk -pdf paper.tex > /dev/null
mv $(SRC_TEX_DIR)/paper.pdf .
@make leave --quiet MESSAGE="paper.pdf generated"
For now, you have no latex sources in tex-sources. You can bring latex code from paper.tar.gz. Read all the sources (starting from paper.tex).
Try
The params.tex file is lacking... add a target for generating it (from params.py) and keeping it up to date.
Important remarks
Now, with the current organization
- you have the main code for your research gathered in a python package. It can be installed with
pipand can be imported in any python scripts. - you have a
params.pyfile that gathers all the parameters used by the figures. It is dedicated to be imported in figure scripts, but it can also be executed in order to generate definitions for latex documents. - you have elementary python scripts for generating each figure you need. They are all named
fig-*.pyso that you can apply a generic makefile rule for each one. - you have well organized latex sources. This enables to share definition between different documents (e.g.
title-matter.tex,equations.tex). Moreover, each part of the article has its own dedicated source (we only havepaper-orbit.texhere). This is very convenient for collaborating on different part of the paper with git, reducing the merging conflicts. - When you change things somewhere, only the relevant information is recomputed. Check this !
- Coherence between all generated information (paper, experiments, ...) is ensured by having the information registered once (e.g. parameters).
Writing the slideshow
As you may have guessed, we will write the slideshow with latex as well, using the beamer package. Some .tex sources will be re-used for the slideshow, these are the ones we have gathered as COMMON_* variables in the makefile.
In the tex-sources directory, add the following files slideshow.tar.gz. Update your makefile in order to compile the slideshow (this is very similar to what you already have for the paper).
Read the files (starting from slideshow.tex), see how things are aligned.
Modify the document class declaration in slideshow.tex as follows and re-generate the slideshow.
Tha layout is preserved. Nice isn't it ?
Add bibliography
Create a biblio.bib file in the tex-sources directory. Add these entries in it
@Article{lorentz,
author = {Edward N. Lorentz},
title = {Deterministic Nonperiodic Flow},
journal = {Journal of the Atmospheric Sciences},
year = {1963},
volume = {20},
pages = {130--141}
}
@Book{chaosbook,
author = {Morris W. Hirsch and Stephen Smale and Robert L. Devaney},
title = {Differential Equations, Dynamical Systems, and an Introduction to Chaos},
publisher = {Academic Press},
year = {2012},
edition = {3rd},
}
Uncomment the lines related to bibliography in paper.tex and paper-orbit.tex. Add the dependancy to biblio.bib in the paper.pdf rule of your makefile. Then, recompile the paper. Check the generation of the bibliography.
Try to change the bibliography style in paper.tex, you can test style apalike instead of unsrt.
Uncomment the input related to bibliography in slideshow.tex and regenerate the slideshow.
Extras
Use it for real
Try this method on one of your projects. You will learn a lot. Ask for help if needed.
More parametrization (not mandatory)
This paragraph is advertising for the notabene package I have developped. It enables to customize your mathematical notations in a smart way. Combined with makefile, you get a powerfull and reliable way to handle your scientifical writings.
Solution
my-research.tar.gz is an example of what can be done.